package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.fragment.NewSearchHotFragment;
import com.longcai.piaojia.fragment.SearchHotFragment;
import com.longcai.piaojia.fragment.SearchLastFragment;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.util.UtilKeyBoard;

/**
 * Created by Administrator on 2016/8/2.
 */
public class SearchActivity extends BaseActivity implements View.OnClickListener {

    private NewSearchHotFragment searchHotFragment;
    private SearchLastFragment searchLastFragment;

    private Fragment[] fragments;

    private int index;
    private int currentTabIndex;

    @BoundView(R.id.search_title_et) public EditText search_title_et;
    @BoundView(R.id.search_title_ll) private LinearLayout search_title_ll;
    @BoundView(R.id.search_title_clear_ll) private LinearLayout search_title_clear_ll;
    @BoundView(R.id.search_title_rl) private RelativeLayout search_title_rl;
    @BoundView(R.id.search_hot_tv) private TextView search_hot_tv;
    @BoundView(R.id.search_last_tv) private TextView search_last_tv;
    @BoundView(R.id.title_back_ll) private LinearLayout title_back_ll;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        setContentView(R.layout.activity_search);

        searchHotFragment = new NewSearchHotFragment();
        searchLastFragment = new SearchLastFragment();

        fragments = new Fragment[]{searchHotFragment, searchLastFragment};

        getSupportFragmentManager().beginTransaction().add(R.id.search_content, searchHotFragment)
                .show(searchHotFragment).commit();

        search_title_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {

//                    closeKeybord();

                    UtilKeyBoard.closeKeybord(search_title_et, getApplicationContext());

                    String str = search_title_et.getText().toString().trim().replaceAll(" ", "");

                    if (TextUtils.isEmpty(str)) {

                        search("");

                        return false;

                    }

                    String searchNotes = BaseApplication.BasePreferences.readSearch();

                    if (searchNotes.length() == 0) {

                        if (!searchNotes.contains(str)) {

                            String newStringNotes = str + "," + searchNotes;

                            BaseApplication.BasePreferences.saveSearch(newStringNotes);

                        }

                    } else {

                        boolean isHad = true;

                        String[] searchArray = searchNotes.split(",");

                        for (int i = 0; i < searchArray.length; i++) {

                            if (searchArray[i].contains(str)) {

                                isHad = false;

                            }

                        }

                        if (isHad) {

                            String newStringNotes = str + "," + searchNotes;

                            BaseApplication.BasePreferences.saveSearch(newStringNotes);

                        } else {

                            String StringNotes = "";

                            for (int i = 0; i < searchArray.length; i++) {

                                if (!searchArray[i].equals(str)) {

                                    StringNotes = StringNotes + "," + searchArray[i];

                                }

                            }

                            String newStringNotes = str + StringNotes + ",";

                            BaseApplication.BasePreferences.saveSearch(newStringNotes);

                        }

                    }

                    if (SearchLastFragment.is_live) {

                        try {
                            ((SearchLastFragment.RefreshSearchNotes) getAppCallBack(SearchLastFragment.class)).refresh();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }

                    search(str);

                    return true;
                }
                return false;
            }
        });

        search_hot_tv.setOnClickListener(this);
        search_last_tv.setOnClickListener(this);
        title_back_ll.setOnClickListener(this);
        search_title_clear_ll.setOnClickListener(this);


//        Log.e("---------key---->", getIntent().getStringExtra("key"));

        search(getIntent().getStringExtra("key"));

    }

    public void closeKeybord(){

        searchHotFragment.searchStr = "";

        UtilKeyBoard.closeKeybord(search_title_et, getApplicationContext());
        search_title_rl.setClickable(true);
        search_title_et.setVisibility(View.GONE);
        search_title_clear_ll.setVisibility(View.GONE);
        search_title_ll.setVisibility(View.VISIBLE);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.title_back_ll:

//                closeKeybord();

                UtilKeyBoard.closeKeybord(search_title_et, getApplicationContext());

                finish();

                break;

            case R.id.search_title_rl:

                search_title_rl.setClickable(false);
                search_title_ll.setVisibility(View.GONE);
                search_title_et.setVisibility(View.VISIBLE);
                search_title_clear_ll.setVisibility(View.VISIBLE);
                search_title_et.setFocusable(true);
                search_title_et.setFocusableInTouchMode(true);
                search_title_et.requestFocus();
                search_title_et.setText("");
                UtilKeyBoard.openKeybord(search_title_et, getApplicationContext());

//                search_title_et.requestFocus

                break;

            case R.id.search_title_clear_ll:

                closeKeybord();

                break;

            case R.id.search_hot_tv:

//                closeKeybord();

                UtilKeyBoard.closeKeybord(search_title_et, getApplicationContext());

                index = 0;

                if (currentTabIndex != index) {

                    search_hot_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_color));
                    search_last_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));

                    FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
                    trx.hide(fragments[currentTabIndex]);
                    if (!fragments[index].isAdded()) {
                        trx.add(R.id.search_content, fragments[index]);
                    }
                    trx.show(fragments[index]).commit();

                    currentTabIndex = index;

                }

                break;

            case R.id.search_last_tv:

//                closeKeybord();

                UtilKeyBoard.closeKeybord(search_title_et, getApplicationContext());

                index = 1;

                if (currentTabIndex != index) {

                    search_hot_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                    search_last_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_color));

                    FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
                    trx.hide(fragments[currentTabIndex]);
                    if (!fragments[index].isAdded()) {
                        trx.add(R.id.search_content, fragments[index]);
                    }
                    trx.show(fragments[index]).commit();

                    currentTabIndex = index;

                }

                break;

        }

    }

    public void search(String str){

        searchHotFragment.searchStr = str;

        search_hot_tv.performClick();

        searchHotFragment.lists_search.clear();

        searchHotFragment.searchResultGet.title = str;
        searchHotFragment.searchResultGet.page = 1;
        searchHotFragment.searchResultGet.execute(SearchActivity.this);


        search_title_rl.setClickable(false);
        search_title_ll.setVisibility(View.GONE);
        search_title_et.setVisibility(View.VISIBLE);
        search_title_clear_ll.setVisibility(View.VISIBLE);

        search_title_et.setText(str);
        search_title_et.setSelection(str.length());

//        //获取焦点
//        search_title_et.setFocusable(true);
//        search_title_et.setFocusableInTouchMode(true);
//        search_title_et.requestFocus();

//        search_title_et.setSelection(str.length()-1);
//
//        UtilKeyBoard.closeKeybord(search_title_et, getApplicationContext());


    }

}
